package com.gofunbuy.app.store;

import com.gofunbuy.R;
import com.gofunbuy.TabStoreGroup;
import com.gofunbuy.core.OrderEntity;
import com.gofunbuy.core.ProductEntity;
import com.gofunbuy.util.AsyncImageLoader;
import com.gofunbuy.util.AsyncImageLoader2.ImageCallback;
import com.gofunbuy.util.GoFunBuyUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class SubTabStoreProductDetailActivity extends Activity {
	// http://www.eye4gift.com/gofun/app_get_product_detail.php?id=16
	private ImageView mImg;
	private TextView mJp, mDonate, mName, mDes, mPrice;
	private LinearLayout mOrder;
	private Spinner mNum;
	private ProductEntity mProduct;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.store_detail_layout);	
		
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();		
		
		mProduct = TabStoreActivity.currentProduct;
		init();
	}

	private void init(){
		
		TextView tvTitle = (TextView) findViewById(R.id.store_topbar_title);
		tvTitle.setText(R.string.title_network_store);
		
		TextView back = (TextView)findViewById(R.id.store_topbar_back);
		back.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(TabStoreActivity.class,null);
			}			
		});
		
		TextView buyList = (TextView) findViewById(R.id.store_topbar_buylist);
		buyList.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(SubTabStoreOrderActivity.class,null);
			}			
		});

		mNum = new Spinner(TabStoreGroup.group);
		mNum.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));		
		LinearLayout numLay = (LinearLayout)findViewById(R.id.store_detail_num);
		numLay.removeViewAt(numLay.getChildCount()-1);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(TabStoreGroup.group,android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.store_order_num));  
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
        mNum.setAdapter(adapter); 
        mNum.setSelection(1);
		numLay.addView(mNum);
		
		mImg = (ImageView)findViewById(R.id.store_detail_img);	
		mJp = (TextView)findViewById(R.id.store_detail_jp);	
		mDonate = (TextView)findViewById(R.id.store_detail_donate);	
		mName = (TextView)findViewById(R.id.store_detail_name);
		mPrice = (TextView)findViewById(R.id.store_detail_price);
		mDes = (TextView)findViewById(R.id.store_detail_des);
		
		
		mOrder = (LinearLayout)findViewById(R.id.store_detail_order);
		mOrder.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int num = mNum.getSelectedItemPosition();
				Log.d("ooooo",mNum.getSelectedItemPosition()+" "+mNum.getSelectedItemId());
				for(int i=0;i<GoFunBuyUtils.mOrders.size();i++){
					if(GoFunBuyUtils.mOrders.get(i).getPe().equals(mProduct)){
						int to = num+GoFunBuyUtils.mOrders.get(i).getNum();
						
						GoFunBuyUtils.mOrders.get(i).setNum(to>10?10:to);
						startActivity(SubTabStoreOrderActivity.class,null);
						return;
					}
				}
				GoFunBuyUtils.mOrders.add(new OrderEntity(num,mProduct));
				startActivity(SubTabStoreOrderActivity.class,null);
			}			
		});
		//AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
		Drawable cachedImage = GoFunBuyUtils.asyncImageLoader.loadBigDrawable(mProduct.getImgUri(),
			new ImageCallback() {
				public void imageLoaded(Drawable imageDrawable,
						String imageUrl) {
					Log.d("pppp",imageDrawable.getIntrinsicWidth()+","+imageDrawable.getIntrinsicHeight());
					mImg.setLayoutParams(new android.widget.LinearLayout.LayoutParams(imageDrawable.getIntrinsicWidth()*5,imageDrawable.getIntrinsicHeight()*5));
					mImg.setImageDrawable(imageDrawable);
				}
		});
		if (cachedImage == null) {
			mImg.setImageResource(R.drawable.loading);
		} else {
			Log.d("pppp2",cachedImage.getIntrinsicWidth()+","+cachedImage.getIntrinsicHeight());
			mImg.setLayoutParams(new android.widget.LinearLayout.LayoutParams(cachedImage.getIntrinsicWidth()*5,cachedImage.getIntrinsicHeight()*5));
			mImg.setImageDrawable(cachedImage);
		}		
		
		mJp.setText(mProduct.getJp());
		mDonate.setText("$"+mProduct.getDonation());
		mName.setText(mProduct.getName());
		mDes.setText(mProduct.getDep());
		mPrice.setText("HK$："+mProduct.getPrice());

	}

	private void startActivity(Class<?> a, Bundle b) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(SubTabStoreProductDetailActivity.this, a);
		if(b!=null){
			intent.putExtras(b);
		}
		
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabStoreGroup.group.getLocalActivityManager().startActivity(
				a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabStoreGroup.group.setContentView(view);
	}

}
